package dao;

import entity.Ticket;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TicketDaoImpl implements ITicketDao {
    private static Map<String, Ticket> ticketMap = new ConcurrentHashMap<>();

    @Override
    public boolean sellTicket(Ticket ticket) {
        if (ticketMap.containsKey(ticket.getTicketId())) {
            return false;
        }
        ticketMap.put(ticket.getTicketId(), ticket);
        return true;
    }

    @Override
    public boolean refundTicket(String electronicTicketId) {
        for (Ticket ticket : ticketMap.values()) {
            if (ticket.getElectronicTicketId().equals(electronicTicketId)) {
                ticket.setStatus("已退票");
                return true;
            }
        }
        return false;
    }

    @Override
    public Ticket getTicketByElectronicId(String electronicTicketId) {
        for (Ticket ticket : ticketMap.values()) {
            if (ticket.getElectronicTicketId().equals(electronicTicketId)) {
                return ticket;
            }
        }
        return null;
    }

    @Override
    public List<Ticket> getTicketsByScheduleId(String scheduleId) {
        List<Ticket> result = new ArrayList<>();
        for (Ticket ticket : ticketMap.values()) {
            if (ticket.getSchedule().getScheduleId().equals(scheduleId)) {
                result.add(ticket);
            }
        }
        return result;
    }
}